package team.seekers.hula.question.point.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import team.seekers.hula.auth.service.MindUtils;
import team.seekers.hula.question.point.entity.dao.PointFireDO;
import team.seekers.hula.question.point.mapper.PointFireMapper;

import javax.annotation.Resource;

/**
 * <p>
 * 用户给一个回答点赞 服务实现类
 * </p>
 *
 * @author hbc
 * @since 2021-02-18
 */
@Service
public class PointFireService extends ServiceImpl<PointFireMapper, PointFireDO> {

    @Resource
    private PointService pointService;

    public void firePoint(String pointId,Integer fire) {
        String userId = MindUtils.getUserId();

        if(isFired(pointId,userId)){
            PointFireDO pointFireDO = lambdaQuery().eq(PointFireDO::getPointId, pointId).eq(PointFireDO::getUserId, userId).one();

            pointFireDO.setFire(pointFireDO.getFire()+fire);
            updateById(pointFireDO);
        }else {
            PointFireDO pointFireDO = PointFireDO.builder()
                    .pointId(pointId)
                    .userId(MindUtils.getUserId())
                    .fire(fire).build();
            save(pointFireDO);
        }
        pointService.addFire(pointId,fire);
    }

    //判断是否已经点过赞了，true为已经点过了
    public boolean isFired(String pointId,String userId){
        return lambdaQuery().eq(PointFireDO::getPointId,pointId).eq(PointFireDO::getUserId,userId).count() > 0;
    }
}
